<?php

namespace app\admin\controller\lists;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\Loader;
use think\response\Json;

/**
 * 服务单录入查询需要的字段集合
 *
 * @icon fa fa-circle-o
 */
class Shenhe extends Backend
{

    /**
     * Shenhe模型对象
     * @var \app\admin\model\lists\Shenhe
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\lists\Shenhe;
        $this->view->assign("statusList", $this->model->getStatusList());
        $jigoudata = [''=>'请选择业务来源','直属业务'=>'直属业务','直接业务'=>'直接业务','分支机构'=>'分支机构','合作商'=>'合作商','查勘机构'=>'查勘机构','招商业务'=>'招商业务','合作业务 '=>'合作业务'];
        $this->view->assign('jigoudata', $jigoudata);
        $chexing = Config::get('site.chexing');
        $this->view->assign('chexing', $chexing);
        $xingzhi = Config::get('site.xingzhi');
        $this->view->assign('xingzhi', $xingzhi);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $where1['status'] = 1;
        $list = $this->model
            ->where($where)
            ->where($where1)
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 浏览
     * @param $ids
     * @return void
     * @throws DbException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function details($ids = null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $admin = $this->auth->getUserInfo();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $row['car_register'] = date('Y-m-d',$row['car_register']);
        $row['plan_strattime'] = date('Y-m-d H:i:s',$row['plan_strattime']);
        $row['plan_endtime'] = date('Y-m-d H:i:s',$row['plan_endtime']);
        $this->view->assign('row', $row);
        $select = selectMenus_zuzhi($admin['zuzhi']);
        $this->assign('selectMenu',$select);
        $ren = db('admin')->where('zuzhi','in',$row['yw_class'])->select();
        $this->assign('ren',$ren);
        $ying = db('tong_files')->where('tong_id',$ids)->select();
        $this->assign('file', $ying);
        $yijian = db('yijian')->where('did',$ids)->select();
        $this->assign('yijian',$yijian);
        $this->assign('admin',$admin);
        return $this->view->fetch();
    }

    //退回修改
    public function tuihui(){
        $id = $_POST['id'];
        $data['did'] = $_POST['id'];
        $data['shen_time'] = strtotime($_POST['shenhe_time']);
        $data['shen_peo'] = $_POST['shenhe_peo'];
        if ($_POST['shenhe_yijian'] == ''){
            $this->error('请填写审核意见');
        }else{
            Db::startTrans();
            try {
                $data['shen_yijian'] = $_POST['shenhe_yijian'];
                $data['user_id'] = $this->auth->getUserInfo()['id'];
                $res = Db::name('yijian')->insert($data);
                if($_POST['tong_number']==''){
                    $dataa['status'] = 4;
                    $ress = Db::name('list_one')->where('id',$id)->update($dataa);
                } else{
                    $dataa['status'] = 4;
                    $ress = Db::name('list_three')->where('id',$id)->update($dataa);
                }
                if($ress){
                    Db::commit();
                    $this->success('成功','');
                }else{
                    Db::rollback();
                    $this->error('失败','');
                }
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    //初级提交审核方法
    public function chuji(){
        //  dump($_POST);die;
        $id = $_POST['id'];
        $post = $_POST['row'];
        if(!$post['zhifu']){
            $this->error('请选择支付方式');
        }
        $data['did'] = $_POST['id'];
        $data['shen_time'] = strtotime($_POST['shenhe_time']);
        $data['shen_peo'] = $_POST['shenhe_peo'];
        $data['user_id'] = $this->auth->getUserInfo()['id'];
        if ($_POST['shenhe_yijian'] == ''){
            $data['shen_yijian'] = "初审通过";
        }else{
            $data['shen_yijian'] = $_POST['shenhe_yijian'];
        }
        $list_one = Db::name('list_one')->where('id',$id)->find();
        $data_list_one = [
            'main_sumdiscount'=>$list_one['main_sumdiscount'],
            'main_money' => $list_one['main_money'],
            'main_standard' => $list_one['main_standard'],
            'main_discount' => $list_one['main_discount'],
            'main_submit' => $list_one['main_submit'],
            'main_santong' => $list_one['main_santong'],
            'main_biao' => $list_one['main_biao'],
            'main_zhe' => $list_one['main_zhe'],
            'main_yingjiao' => $list_one['main_yingjiao'],
            'siji_tong' => $list_one['siji_tong'],
            'siji_biao' => $list_one['siji_biao'],
            'siji_zhe' => $list_one['siji_zhe'],
            'siji_ying' => $list_one['siji_ying'],
            'ck_money' => $list_one['ck_money'],
            'chengke_zuo' => $list_one['chengke_zuo'],
            'ck_tong' => $list_one['ck_tong'],
            'ck_biao' => $list_one['ck_biao'],
            'ck_zhe' => $list_one['ck_zhe'],
            'ck_ying' => $list_one['ck_ying'],
            'dao_tong' => $list_one['dao_tong'],
            'dao_biao' => $list_one['dao_biao'],
            'dao_zhe' => $list_one['dao_zhe'],
            'dao_ying' => $list_one['dao_ying'],
            'ziran_c' => $list_one['ziran_c'],
            'zi_tong' => $list_one['zi_tong'],
            'zi_biao' => $list_one['zi_biao'],
            'zi_zhe' => $list_one['zi_zhe'],
            'zi_ying' => $list_one['zi_ying'],
            'fd_c' => $list_one['fd_c'],
            'fdj_tong' => $list_one['fdj_tong'],
            'fdj_biao' => $list_one['fdj_biao'],
            'fdj_zhe' => $list_one['fdj_zhe'],
            'fdj_ying' => $list_one['fdj_ying'],
            'zd_tong' => $list_one['zd_tong'],
            'zd_biao' => $list_one['zd_biao'],
            'zd_zhe' => $list_one['zd_zhe'],
            'zd_ying' => $list_one['zd_ying'],
            'huowu_c' => $list_one['huowu_c'],
            'hw_tong' => $list_one['hw_tong'],
            'hw_biao' => $list_one['hw_biao'],
            'hw_zhe' => $list_one['hw_zhe'],
            'hw_ying' => $list_one['hw_ying'],
            'boli_c' => $list_one['boli_c'],
            'bl_tong' => $list_one['bl_tong'],
            'bl_biao' => $list_one['bl_biao'],
            'bl_zhe' => $list_one['bl_zhe'],
            'bl_ying' => $list_one['bl_ying'],
            'bu_tong' => $list_one['bu_tong'],
            'bu_biao' => $list_one['bu_biao'],
            'bu_zhe' => $list_one['bu_zhe'],
            'bu_ying' => $list_one['bu_ying'],
            'main_sum' => $list_one['main_sum'],
            'main_free' => $list_one['main_free'],
            'dw_tong' => $list_one['dw_tong'],
            'dw_biao' => $list_one['dw_biao'],
            'dw_zhe' => $list_one['dw_zhe'],
            'dw_ying' => $list_one['dw_ying'],
        ];
        $dataa = [
            'main_sumdiscount'=>$post['main_sumdiscount'],
            'main_money' => $post['main_money'],
            'main_standard' => $post['main_standard'],
            'main_discount' => $post['main_discount'],
            'main_submit' => $post['main_submit'],
            'main_santong' => $post['main_santong'],
            'main_biao' => $post['main_biao'],
            'main_zhe' => $post['main_zhe'],
            'main_yingjiao' => $post['main_yingjiao'],
            'siji_tong' => $post['siji_tong'],
            'siji_biao' => $post['siji_biao'],
            'siji_zhe' => $post['siji_zhe'],
            'siji_ying' => $post['siji_ying'],
            'ck_money' => $post['ck_money'],
            'chengke_zuo' => $post['chengke_zuo'],
            'ck_tong' => $post['ck_tong'],
            'ck_biao' => $post['ck_biao'],
            'ck_zhe' => $post['ck_zhe'],
            'ck_ying' => $post['ck_ying'],
            'dao_tong' => $post['dao_tong'],
            'dao_biao' => $post['dao_biao'],
            'dao_zhe' => $post['dao_zhe'],
            'dao_ying' => $post['dao_ying'],
            'main_sum' => $post['main_sum'],
            'main_free' => $post['main_free'],
            'yw_peo' => $post['yw_peo'],
            'yw_peo_phone' => $post['yw_peo_phone'],
            'kehu_sumdiscount' => $post['main_sumdiscount'],
            'special' => $post['special'],
            'dw_tong' => $post['dw_tong'],
            'dw_biao' => $post['dw_biao'],
            'dw_zhe' => $post['dw_zhe'],
            'dw_ying' => $post['dw_ying'],
            'zhifu' => $post['zhifu'],
        ];
        Db::startTrans();
        try {
            $res = Db::name('yijian')->insert($data);
            $list_one_hf = Db::name('list_one_hf')->where('one_id',$id)->find();
            if($list_one_hf){
                Db::name('list_one_hf')->where('one_id',$id)->update($data_list_one);
            }else{
                $data_list_one['one_id'] = $id;
                Db::name('list_one_hf')->insert($data_list_one);
            }
            $dataa['status'] = 3;
            $dataa['yw_hetong_time'] = strtotime($_POST['shenhe_time']);
            $dataa['yw_hetongpeo'] = $_POST['shenhe_peo'];
            $ress = Db::name('list_one')->where('id',$id)->update($dataa);
            if($res){
                Db::commit();
                $this->success('操作成功');
            }else{
                Db::rollback();
                $this->error('操作失败');
            }
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

}
